﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.Object;
using Model.DataContext;

namespace Nongsan.Controllers
{
    public class HoaDonController : Controller
    {
        private DataContext db = new DataContext();

        //abc
        // GET: /HoaDon/
       public ActionResult jsonList()
        {


            var list = from n in db.HoaDons select new {n.Tong_tien,n.Phi_ship,n.Ngay_thang,n.Trang_thai,n.SDT_nhan,n.Dia_chi_nhan,n.Ho_ten_nhan,n.Ghi_chu,n.Email_nhan,n.Nguoidung.Ho_ten,n.ID };
			
            return Json(list, JsonRequestBehavior.AllowGet);
        } 
		
        public ActionResult Index()
        {

            return View();

        }

        //
        // GET: /HoaDon/Details/5

        public ActionResult Details(int id = 0)
        {
            HoaDon hoadon = db.HoaDons.Find(id);
            if (hoadon == null)
            {
                return HttpNotFound();
            }
            return View(hoadon);
        }

        //
        // GET: /HoaDon/Create

        public ActionResult Create()
        {
            ViewBag.ID_Nguoidung = new SelectList(db.NguoiDungs, "ID", "User_Name");
            return View();
        }

        //
        // POST: /HoaDon/Create
		   [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(HoaDon hoadon)
        {
            if (ModelState.IsValid)
            {
                db.HoaDons.Add(hoadon);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ID_Nguoidung = new SelectList(db.NguoiDungs, "ID", "User_Name", hoadon.ID_Nguoidung);
            return View(hoadon);
        }

        //
        // GET: /HoaDon/Edit/5

        public ActionResult Edit(int id = 0)
        {
            HoaDon hoadon = db.HoaDons.Find(id);
            if (hoadon == null)
            {
                return HttpNotFound();
            }
            ViewBag.ID_Nguoidung = new SelectList(db.NguoiDungs, "ID", "User_Name", hoadon.ID_Nguoidung);
            return View(hoadon);
        }

        //
        // POST: /HoaDon/Edit/5
		   [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(HoaDon hoadon)
        {
            if (ModelState.IsValid)
            {
                db.Entry(hoadon).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ID_Nguoidung = new SelectList(db.NguoiDungs, "ID", "User_Name", hoadon.ID_Nguoidung);
            return View(hoadon);
        }

        //
        // GET: /HoaDon/Delete/5

        public ActionResult Delete(int id = 0)
        {
            HoaDon hoadon = db.HoaDons.Find(id);
            if (hoadon == null)
            {
                return HttpNotFound();
            }
            return View(hoadon);
        }

        //
        // POST: /HoaDon/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            HoaDon hoadon = db.HoaDons.Find(id);
            db.HoaDons.Remove(hoadon);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
		[HttpPost]
        public ActionResult ajDelete(int id)
        {
             HoaDon hoadon = db.HoaDons.Find(id);
			 if (hoadon == null)
            {
                return Json(new { success = false ,msg="Không tìm thấy đối tượng"});
            }

         
            db.HoaDons.Remove(hoadon);
			db.SaveChanges();
            return Json(new { success=true});
        }
		
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}